/**
 * 资源管理相关的API服务
 */

import {
  httpGet,
  httpPost,
  httpPut,
  httpDelete,
  httpUpload,
} from "../utils/http";

// 资源接口
export interface Resource {
  id: string;
  name: string;
  type: "ppt" | "pdf" | "doc" | "video" | "audio" | "image" | "folder";
  size: string;
  uploadTime: string;
  category: "课件" | "实验资料" | "试题资料" | "视频资源" | "其他";
  downloads: number;
  shared: boolean;
  description?: string;
  url?: string;
  uploaderId: string;
  uploaderName: string;
  courseId?: string;
  classId?: string;
}

// 文件上传进度接口
export interface UploadProgress {
  fileId: string;
  fileName: string;
  progress: number;
  status: "uploading" | "success" | "error";
  error?: string;
}

// 资源管理API
export const resourceAPI = {
  // 获取资源列表
  async getResources(params?: {
    page?: number;
    size?: number;
    category?: string;
    type?: string;
    keyword?: string;
    courseId?: string;
    classId?: string;
    shared?: boolean;
  }) {
    return httpGet("/api/v1/tea/resources", params);
  },

  // 获取资源详情
  async getResource(id: string) {
    return httpGet(`/api/v1/tea/resources/${id}`);
  },

  // 创建资源记录（不包含文件上传）
  async createResource(
    data: Omit<Resource, "id" | "uploadTime" | "downloads">
  ) {
    return httpPost("/api/v1/tea/resources", data);
  },

  // 更新资源信息
  async updateResource(id: string, data: Partial<Resource>) {
    return httpPut(`/api/v1/tea/resources/${id}`, data);
  },

  // 删除资源
  async deleteResource(id: string) {
    return httpDelete(`/api/v1/tea/resources/${id}`);
  },

  // 批量删除资源
  async deleteResources(ids: string[]) {
    return httpDelete("/api/v1/tea/resources/batch", { data: { ids } });
  },

  // 上传文件
  async uploadFile(
    file: File,
    metadata: {
      category: Resource["category"];
      description?: string;
      courseId?: string;
      classId?: string;
      shared?: boolean;
    },
    onProgress?: (progress: number) => void
  ) {
    const formData = new FormData();
    formData.append("file", file);
    formData.append("metadata", JSON.stringify(metadata));

    return httpUpload("/api/v1/tea/resources/upload", file, onProgress);
  },

  // 批量上传文件
  async uploadFiles(
    files: File[],
    metadata: {
      category: Resource["category"];
      description?: string;
      courseId?: string;
      classId?: string;
      shared?: boolean;
    },
    onProgress?: (fileId: string, progress: number) => void
  ) {
    const uploadPromises = files.map((file) =>
      this.uploadFile(file, metadata, (progress) => {
        onProgress?.(file.name, progress);
      })
    );

    return Promise.all(uploadPromises);
  },

  // 下载资源
  async downloadResource(id: string) {
    return httpGet(
      `/api/v1/tea/resources/${id}/download`,
      {},
      {
        responseType: "blob",
      }
    );
  },

  // 预览资源
  async previewResource(id: string) {
    return httpGet(`/api/v1/tea/resources/${id}/preview`);
  },

  // 分享/取消分享资源
  async toggleShare(id: string, shared: boolean) {
    return httpPut(`/api/v1/tea/resources/${id}/share`, { shared });
  },

  // 获取资源统计信息
  async getResourceStats(params?: {
    courseId?: string;
    classId?: string;
    timeRange?: string;
  }) {
    return httpGet("/api/v1/tea/resources/stats", params);
  },

  // 搜索资源
  async searchResources(params: {
    keyword: string;
    type?: string;
    category?: string;
    courseId?: string;
    classId?: string;
    page?: number;
    size?: number;
  }) {
    return httpGet("/api/v1/tea/resources/search", params);
  },

  // 获取资源目录树
  async getResourceTree(params?: { courseId?: string; classId?: string }) {
    return httpGet("/api/v1/tea/resources/tree", params);
  },

  // 创建文件夹
  async createFolder(data: {
    name: string;
    parentId?: string;
    courseId?: string;
    classId?: string;
    description?: string;
  }) {
    return httpPost("/api/v1/tea/resources/folder", data);
  },

  // 移动资源到文件夹
  async moveResource(resourceId: string, folderId: string) {
    return httpPut(`/api/v1/tea/resources/${resourceId}/move`, { folderId });
  },

  // 复制资源
  async copyResource(id: string, targetFolderId?: string) {
    return httpPost(`/api/v1/tea/resources/${id}/copy`, { targetFolderId });
  },

  // 获取最近上传的资源
  async getRecentResources(limit: number = 10) {
    return httpGet("/api/v1/tea/resources/recent", { limit });
  },

  // 获取热门下载资源
  async getPopularResources(limit: number = 10) {
    return httpGet("/api/v1/tea/resources/popular", { limit });
  },
};
